using System;
using System.Collections;
using System.Collections.Generic;
using AdvancedInspector;
using I2.Loc;
using UnityEngine;
using UnityEngine.SceneManagement;

[AdvancedInspector(true)]
public class ManagerMissions : MonoBehaviour
{
	public const string keyArrAccessMissions = "keyArrAccessMissions";

	public MissionHUD hud;

	public static ManagerMissions instance;

	public bool isStarted;

	public List<MissionBase> listMission = new List<MissionBase>();

	public List<MissionBase> startedMission = new List<MissionBase>();

	public List<string> listIdCurMissions = new List<string>();

	public static MissionBase currMission;

	public int MaxCountAccessMissions = 1;

	private List<string> listIDNewMissions = new List<string>();

	public static event Action<EnemyBehavior> killMob;

	public static event Action killPlayer;

	public static event Action startMission;

	public static event Action updateTarget;

	public static event Action completeTarget;

	public static event Action completeMission;

	public static event Action stopMission;

	private void Awake()
	{
		instance = this;
		string[] array = Load.LoadStringArray("keyArrAccessMissions");
		listIdCurMissions.Clear();
		if (BCWDebug.isDebugBuild)
		{
			BCWDebug.Log("Awake listIdCurMissions=" + listIdCurMissions.Count);
		}
	}

	private void Start()
	{
		if (hud == null)
		{
			hud = GameTopWindow.instance.missionHud;
		}
	}

	private void OnEnable()
	{
		if (hud == null && GameTopWindow.instance != null)
		{
			hud = GameTopWindow.instance.missionHud;
		}
	}

	private void OnDestroy()
	{
		instance = null;
	}

	public void ClearAllMissions()
	{
		foreach (MissionBase item in listMission)
		{
			item.ResetMission();
		}
	}

	private void Update()
	{
		if (!isStarted || startedMission == null)
		{
			return;
		}
		for (int i = 0; i < startedMission.Count; i++)
		{
			MissionBase missionBase = startedMission[i];
			if (missionBase != null && !missionBase.isCompleted)
			{
				missionBase.OnUpdateMission();
			}
		}
	}

	public void LocalizeMissionText()
	{
		foreach (MissionBase item in startedMission)
		{
			ScriptLocalization.SetMissionLocalizatiion(item);
		}
	}

	public void GetListMission()
	{
		List<MissionBase> list = new List<MissionBase>();
		list.AddRange(Resources.LoadAll<MissionBase>("MyMissions/MissionObj"));
		listMission = list;
	}

	public void ForceStartMissionWithNumberInArray(int indexMission)
	{
		List<MissionBase> list = startedMission;
		for (int i = 0; i < list.Count; i++)
		{
			instance.RemoveIdCurrentMissions(list[i]);
		}
		for (int j = 0; j < indexMission; j++)
		{
			foreach (TargetParameter listTargetParam in listMission[j].listTargetParams)
			{
				listTargetParam.SetIsCompletedTarget(true);
			}
			settings.CathedExeptions += listMission[j].CountAndAddExeptions;
		}
		for (int k = indexMission; k < listMission.Count; k++)
		{
			listMission[k].ResetMission();
		}
		MissionBase missionBase = listMission[indexMission];
		BCWDebug.Log("Force start mission: " + missionBase.name);
		string[] variable = new string[1] { missionBase.name };
		listIdCurMissions.Clear();
		listIdCurMissions.Add(missionBase.name);
		Save.SaveStringArray("keyArrAccessMissions", variable);
		StartAllCurrentMissions();
	}

	public void StartAllCurrentMissions()
	{
		if (listIdCurMissions != null && listIdCurMissions.Count > 0)
		{
			foreach (string listIdCurMission in listIdCurMissions)
			{
				StartMissionWithId(listIdCurMission);
			}
		}
		LocalizeMissionText();
	}

	public void StopAllMission()
	{
		for (int i = 0; i < startedMission.Count; i++)
		{
			startedMission[i].OnStopMission();
			UnityEngine.Object.Destroy(startedMission[i].gameObject);
		}
		if (ManagerMissions.stopMission != null)
		{
			ManagerMissions.stopMission();
		}
		startedMission.Clear();
	}

	public MissionBase StartMissionWithId(string val, bool reset = false)
	{
		bool flag = false;
		StopAllMission();
		hud.HideMissionCompleate();
		if (startedMission != null)
		{
			foreach (MissionBase item in startedMission)
			{
				if (item != null && item.gameObject.name == val)
				{
					flag = true;
					break;
				}
			}
		}
		if (flag)
		{
			return null;
		}
		List<string> list = new List<string>();
		foreach (MissionBase item2 in listMission)
		{
			if (!(val == item2.gameObject.name))
			{
				continue;
			}
			if (item2.NeedForceStop)
			{
				if (listIdCurMissions.Contains(val))
				{
					listIdCurMissions.Remove(val);
				}
				return null;
			}
			if (GameController.instance != null)
			{
				isStarted = true;
			}
			return InstMission(item2, reset);
		}
		return null;
	}

	public MissionBase StartMission(MissionBase val, bool reset = false)
	{
		bool flag = false;
		StopAllMission();
		hud.HideMissionCompleate();
		if (startedMission != null)
		{
			foreach (MissionBase item in startedMission)
			{
				if (item != null && item.gameObject.name == val.name)
				{
					flag = true;
					break;
				}
			}
		}
		if (flag)
		{
			return null;
		}
		return InstMission(val, reset);
	}

	private MissionBase InstMission(MissionBase needMission, bool reset = false)
	{
		if (GameController.instance != null)
		{
			if (!string.IsNullOrEmpty(needMission.nameScene) && needMission.nameScene != SceneManager.GetActiveScene().name)
			{
				return null;
			}
			if (needMission.offlineMission != settings.offlineMode)
			{
				return null;
			}
		}
		GameObject gameObject = UnityEngine.Object.Instantiate(needMission.gameObject);
		MissionBase component = gameObject.GetComponent<MissionBase>();
		gameObject.transform.parent = base.transform;
		gameObject.transform.localPosition = Vector3.zero;
		gameObject.transform.localRotation = Quaternion.identity;
		gameObject.gameObject.name = needMission.gameObject.name;
		ScriptLocalization.SetMissionLocalizatiion(component);
		if (reset)
		{
			component.ResetMission();
		}
		currMission = component;
		startedMission.Add(component);
		component.OnStartMission();
		if (ManagerMissions.startMission != null)
		{
			ManagerMissions.startMission();
		}
		if (ManagerMissions.updateTarget != null)
		{
			ManagerMissions.updateTarget();
		}
		return component;
	}

	public bool ExistStartedMissions()
	{
		if (startedMission != null && startedMission.Count > 0)
		{
			return true;
		}
		return false;
	}

	public void MissionCompleteSuccess(string val)
	{
		hud.ShowMissionCompleate(currMission);
		MissionBase missionBase = null;
		foreach (MissionBase item in startedMission)
		{
			if (val == item.gameObject.name)
			{
				missionBase = item;
				break;
			}
		}
		if (missionBase != null)
		{
			startedMission.Remove(missionBase);
		}
	}

	public void MissionFailed()
	{
	}

	public void CheckIsExistCompleteMissions()
	{
		if (BCWDebug.isDebugBuild)
		{
			BCWDebug.Log("CheckIsExistCompleteMissions");
		}
		MissionBase curCompleteMission = GetCurCompleteMission();
		if (curCompleteMission != null)
		{
			ManagerHUDMissionLobby.instance.ShowWindowCompleteMission(curCompleteMission);
			instance.RemoveIdCurrentMissions(curCompleteMission);
		}
		else
		{
			controllerMenu.thisScript.showMainMenu();
			UpdateListAccessMissions();
		}
	}

	public MissionBase GetCurCompleteMission()
	{
		foreach (MissionBase item in startedMission)
		{
			if (item.MissionIsComplete())
			{
				return item;
			}
		}
		return null;
	}

	public void UpdateListAccessMissions()
	{
		BCWDebug.Log("UpdateListAccessMissions curCount = " + listIdCurMissions.Count);
		if (listIdCurMissions.Count >= MaxCountAccessMissions)
		{
			return;
		}
		listIDNewMissions.Clear();
		foreach (MissionBase item in listMission)
		{
			if (!item.NeedForceStop && !item.MissionIsComplete() && !listIdCurMissions.Contains(item.gameObject.name) && listIdCurMissions.Count < MaxCountAccessMissions)
			{
				listIdCurMissions.Add(item.gameObject.name);
				listIDNewMissions.Add(item.gameObject.name);
			}
		}
		SaveListIdAccessMissions();
		ShowInfoAndStartNewMissions();
	}

	public void ShowInfoAndStartNewMissions()
	{
		BCWDebug.Log("ShowInfoAndStartNewMissions");
		if (listIDNewMissions.Count > 0)
		{
			string text = listIDNewMissions[0];
			MissionBase missionBase = StartMissionWithId(text);
			listIDNewMissions.Remove(text);
			BCWDebug.Log("scriptNewMission = " + missionBase);
			if (missionBase != null)
			{
				ManagerHUDMissionLobby.instance.ShowWindowNewMission(missionBase);
			}
		}
		else
		{
			if (BCWDebug.isDebugBuild)
			{
				BCWDebug.Log("No new missions");
			}
			controllerMenu.thisScript.showMainMenu();
		}
	}

	public void SaveListIdAccessMissions()
	{
		Save.SaveStringArray("keyArrAccessMissions", listIdCurMissions.ToArray());
	}

	public void RemoveIdCurrentMissions(MissionBase needMis)
	{
		if (listIdCurMissions.Contains(needMis.gameObject.name))
		{
			if (BCWDebug.isDebugBuild)
			{
				BCWDebug.Log("RemoveIdCurrentMissions =" + needMis.gameObject.name);
			}
			listIdCurMissions.Remove(needMis.gameObject.name);
			SaveListIdAccessMissions();
		}
		if (startedMission.Contains(needMis))
		{
			startedMission.Remove(needMis);
			StartCoroutine(CheckScrollMission());
		}
	}

	private IEnumerator CheckScrollMission()
	{
		while (!(ManagerScrollMissions.instance != null))
		{
			yield return new WaitForEndOfFrame();
		}
		ManagerScrollMissions.instance.UpdateListMissions();
	}

	public void ShowNewMissionWindow(MissionBase mission)
	{
		ShowMissionFalled(false);
		hud.ShowNewMissionWindow(mission);
	}

	public void ShowMissionCompleate()
	{
		hud.ShowMissionCompleate(currMission);
	}

	public void HideMissionWindow()
	{
		hud.HideMissionWindow();
	}

	public void ShowMissionFalled(bool val)
	{
		if (val)
		{
			hud.ShowMissionFaled(currMission);
		}
		else
		{
			hud.HideMissionFaled();
		}
	}

	public bool MissionIsComplete(string idMis)
	{
		foreach (MissionBase item in startedMission)
		{
			if (idMis == item.gameObject.name)
			{
				return item.MissionIsComplete();
			}
		}
		return false;
	}

	public List<TargetParameter> GetStartedTargetsAllMission()
	{
		List<TargetParameter> list = new List<TargetParameter>();
		foreach (MissionBase item in startedMission)
		{
			if (item.isStarted && !item.isCompleted)
			{
				list.Add(item.StartedTarget());
			}
		}
		return list;
	}

	public static void OnUpdateTargetValue()
	{
		if (ManagerMissions.updateTarget != null)
		{
			ManagerMissions.updateTarget();
		}
	}

	public static void OnMissionFaled()
	{
		instance.ShowMissionFalled(true);
	}

	public static void OnCompleteTarget()
	{
		if (ManagerMissions.completeTarget != null)
		{
			ManagerMissions.completeTarget();
		}
	}

	public static void OnCompleteMission()
	{
		if (ManagerMissions.completeMission != null)
		{
			ManagerMissions.completeMission();
		}
		currMission = null;
	}

	public static void OnKillMob(EnemyBehavior curEnemy = null)
	{
		if (BCWDebug.isDebugBuild)
		{
			BCWDebug.Log("OnKillMob: " + curEnemy);
		}
		if (instance != null && ManagerMissions.killMob != null)
		{
			ManagerMissions.killMob(curEnemy);
		}
	}

	public static void OnKillPlayer()
	{
		if (BCWDebug.isDebugBuild)
		{
			BCWDebug.Log("OnKillPlayer");
		}
		if (instance != null && ManagerMissions.killPlayer != null)
		{
			ManagerMissions.killPlayer();
		}
	}
}
